// pages/p-publish/p-publish.js
import {formatTime} from '../../utils/util'
const api = require('../../utils/api')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isShowPublish: false,
    confirmButton: [{text: '取消'}, {text: '确定'}],
    card: { // 发布的帖子的模板，_id 后台获取
      comment: [],
      commentNum: 0,
      content: '',
      pic: '',
      pinfo: '',
      publishTime: '',
      thumbsNum: 0,
      title: '',
      type: '0',
    }
  },
  showPublish() {    
    this.setData({
      isShowPublish: true
    })
  },
  hidePublish(event) {
    this.setData({
      isShowPublish: false
    })
    // console.log(event);
    const isConfirm = event.detail.index
    // console.log(isConfirm);
    if (isConfirm == 1) {
      const card = {...this.data.card}
      if (!card.title) {
        wx.showToast({
          title: '请填写帖子标题',
          icon: 'none',
          duration: 1000
        })
        return
      }
      if (!card.content) {
        wx.showToast({
          title: '请填写帖子内容',
          icon: 'none',
          duration: 1000
        })
        return
      }
      const date = new Date()
      card.publishTime = formatTime(date)
      this.setData({card})
      // console.log(this.data.card)
      api.addCard(card)

      wx.navigateBack() // 回到我的页面
    }
  },
  formSubmit(e) {
    const {title, content} = e.detail.value
    // console.log(title, content);
    const card = {...this.data.card}
    card.title = title
    card.content = content
    this.setData({card})
  },
  uploadPic() {
    wx.showToast({
      title: '暂不支持上传图片功能',
      mask: true,
      icon: 'none',
      duration: 1000
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})